﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;

namespace FastAccess
{
    [Serializable]
    public class Config
    {
        public bool RunOnStatUp { get; set; }
        public Size ClientSize { get; set; }
        public Point ClientPosition { get; set; }
        public FormWindowState WindowState { get; set; }

        public Config() { }

        #region Serialization

        public void Serialize(string file)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(string.Format("{0}\\{1}", Application.StartupPath, file), FileMode.Create);

                XmlSerializer s = new XmlSerializer(typeof(Config));
                s.Serialize(fs, this);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(ex.Message, ex.StackTrace),
                    "XmlSerializer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (fs != null)
                fs.Close();
        }

        public static Config Deserialize(string file)
        {
            Config cfg = null;
            try
            {
                string fileName = string.Format("{0}\\{1}", Application.StartupPath, file);
                if (File.Exists(fileName))
                {
                    FileStream fs = new FileStream(fileName, FileMode.Open);
                    try
                    {
                        XmlSerializer s = new XmlSerializer(typeof(Config));
                        cfg = (Config)s.Deserialize(fs);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(string.Format(ex.Message, ex.StackTrace),
                            "XmlSerializer", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(ex.Message, ex.StackTrace),
                    "XmlSerializer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (cfg == null)
                return new Config();
            else
                return cfg;
        }

        #endregion
    }
}
